import React from 'react';
import { Tree } from 'antd';

const { TreeNode } = Tree;

class MenuTree extends React.Component {
  state = {
    autoExpandParent: true,
    checkedKeys: []
  };

  componentDidMount() {
    if (this.props.onRef) {
      this.props.onRef(this)
    }
  }

  onCheck = (checkedKeys, e) => {
    // console.log('onCheck', checkedKeys);
    this.setState({ checkedKeys });
  };

  resetCheckedKeys = () => {
    this.setState({ checkedKeys: [] })
  }

  setChecked = data => {
    this.setState({ checkedKeys: data })
  }

  renderTreeNodes = (data) =>
    data.map(item => {
      if (item.children) {
        return (
          <TreeNode title={item.name} key={item.menuId} dataRef={item}>
            {this.renderTreeNodes(item.children)}
          </TreeNode>
        );
      }
      return <TreeNode key={item.menuId} {...item} title={item.name} />;
    });

  render() {
    const { treeData, authority } = this.props;
    return (
      <Tree
        checkable
        onCheck={this.onCheck}
        checkedKeys={this.state.checkedKeys}
        selectedKeys={this.state.selectedKeys}
        defaultExpandAll={true}
        disabled={authority === 1}
      >
        {this.renderTreeNodes(treeData)}
      </Tree>
    )
  }
}

export default MenuTree
